package process2

import (
	message "chat/common"
	"chat/server/models"
	"chat/server/utils"
	"encoding/json"
	"fmt"
	"net"

	"github.com/gomodule/redigo/redis"
)

type UserProcess struct {
	UserId   int
	UserName string
	Conn     net.Conn
}

func (that *UserProcess) SmsOpen(smsMes *message.Message) {
	var resMes message.Message
	resMes.Type = message.SmsMesType
	resMes.Data = smsMes.Data
	fmt.Println("resMes", resMes)
	data, err := json.Marshal(resMes)
	if err != nil {
		return
	}
	tr := &utils.Transfer{
		Conn: that.Conn,
	}
	err = tr.WritePkg(data)
	if err != nil {
		return
	}
}

func (that *UserProcess) NotifyOthersOnlineUser(userInfo *UserProcess) {
	for id, up := range userMgr.onlineUsers {
		if id == userInfo.UserId {
			continue
		}
		up.NotifyMeOnline(userInfo)
	}
}

func (that *UserProcess) NotifyMeOnline(userInfo *UserProcess) {
	var resMes message.Message
	resMes.Type = message.NotifyUserStatusMesType
	var notifyUserStatusMes message.NotifyUserStatusMes
	notifyUserStatusMes.UserId = userInfo.UserId
	notifyUserStatusMes.UserName = userInfo.UserName
	notifyUserStatusMes.Status = message.UserOnline
	data, err := json.Marshal(notifyUserStatusMes)
	if err != nil {
		fmt.Println("反序列化失败，err=", err)
		return
	}
	resMes.Data = string(data)
	data, err = json.Marshal(resMes)
	if err != nil {
		fmt.Println("序列化失败，err=", err)
		return
	}
	tr := &utils.Transfer{
		Conn: that.Conn,
	}
	err = tr.WritePkg(data)
	if err != nil {
		fmt.Println("notifyErr，err=", err)
		return
	}
}
func (that *UserProcess) ServerProcessLogin(mes *message.Message, pool *redis.Pool) (err error) {
	var loginMes message.LoginMes
	err = json.Unmarshal([]byte(mes.Data), &loginMes)
	if err != nil {
		fmt.Println("反序列化失败，err=", err)
		return
	}

	var resMes message.Message
	resMes.Type = message.LoginMesType
	user, err := models.MyUserDao.Login(loginMes.UserId, loginMes.UserPwd)
	var loginResMes message.LoginResMes
	if err != nil {
		loginResMes.Code = 500
		loginResMes.Error = err.Error()
	} else {
		that.UserId = user.UserId
		that.UserName = user.UserName
		userMgr.AddOnineUser(that)
		for id, v := range userMgr.onlineUsers {
			userInfo := message.LoginMes{
				UserId:   id,
				UserName: v.UserName,
			}
			loginResMes.Data = append(loginResMes.Data, userInfo)
		}
		loginResMes.Code = 200
	}
	data, err := json.Marshal(loginResMes)
	if err != nil {
		fmt.Println("序列化失败，err=", err)
	}
	resMes.Data = string(data)
	data, err = json.Marshal(resMes)
	if err != nil {
		fmt.Println("序列化失败，err=", err)
	}
	tr := &utils.Transfer{
		Conn: that.Conn,
	}
	err = tr.WritePkg(data)
	return
}

func (that *UserProcess) ServerProcessRegister(mes *message.Message, pool *redis.Pool) (err error) {
	var registerMes message.RegisterMes
	fmt.Println(mes.Data)
	err = json.Unmarshal([]byte(mes.Data), &registerMes)
	if err != nil {
		fmt.Println("反序列化失败，err=", err)
	}
	var resMes message.Message
	resMes.Type = message.RegisterResMesType
	fmt.Println(registerMes.User)
	err = models.MyUserDao.Register(&registerMes.User)
	if err != nil {
		fmt.Println("err=", err)
	}
	var registerResMes message.LoginResMes
	if err != nil {
		if err == models.Err_USER_EXISTS {
			registerResMes.Code = 500
			registerResMes.Error = models.Err_USER_EXISTS.Error()
		} else {
			registerResMes.Code = 500
			registerResMes.Error = "未知错误"
		}
	} else {

		registerResMes.Code = 200
	}
	data, err := json.Marshal(registerResMes)
	if err != nil {
		fmt.Println("序列化失败，err=", err)
	}
	resMes.Data = string(data)
	data, err = json.Marshal(resMes)
	if err != nil {
		fmt.Println("序列化失败，err=", err)
	}
	tr := &utils.Transfer{
		Conn: that.Conn,
	}
	err = tr.WritePkg(data)
	return
}
